ALTER function FN_GET_PARTNERS (@USRID int)
returns @TABLE table
(
FN_PRT_ID int
)
begin

declare @ID int,
        @BOSS int,
        @PRT_ID int; 

declare @TTABLE table
(
PRT_ID int
)

declare C cursor for 
 select USR_ID, USR_FONOK from _USER where USR_FONOK=@USRID or USR_ID=@USRID

open C
fetch next from C into @ID, @BOSS

while @@FETCH_STATUS = 0 
begin
 insert into @TTABLE select PRT_ID from PARTNER where PRT_CREATED_BY=@ID 
 if @ID<>@USRID 
  insert into @TTABLE select * from FN_GET_PARTNERS(@ID)
 fetch next from C into @ID, @BOSS
end

close C
deallocate C

insert into @TABLE select distinct * from @TTABLE

return
end